package com.weather.controller;

import com.weather.domain.Weather;
import com.weather.service.WeatherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.math.BigInteger;
import java.net.URI;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/weather")
public class WeatherController {


    @Autowired
    private WeatherService weatherService;

    @GetMapping("/abc")
    public String hello() {
        return "Hello World";
    }

    @GetMapping("/def")
    public Map<String, Object> hello2() {
        RestTemplate restTemplate = new RestTemplate();
        Map<String, Object> ret = restTemplate.getForObject(URI.create("https://restapi.amap.com/v3/weather/weatherInfo?city=530102&key=d1636cf5eb16c9ce8e9f5474750a1687"), Map.class);
        System.out.println(ret);

        if (ret != null && ret.containsKey("lives")) {
            // 获取 lives 列表
            @SuppressWarnings("unchecked")
            List<Map<String, Object>> lives = (List<Map<String, Object>>) ret.get("lives");

            if (!lives.isEmpty()) {
                // 获取第一个元素
                Map<String, Object> firstLive = lives.get(0);

                // 打印 city 字段
                System.out.println("City: " + firstLive.get("city"));

                // 如果需要进一步处理 Weather 对象
                Weather weather = new Weather();
                weather.setCity((String) firstLive.get("city"));
                weather.setAdcode((String) firstLive.get("adcode"));
                weather.setProvince((String) firstLive.get("province"));
                weather.setWeather((String) firstLive.get("weather"));
                weather.setTemperature((String) firstLive.get("temperature"));
                weather.setWinddirection((String) firstLive.get("winddirection"));
                weather.setWindpower((String) firstLive.get("windpower"));
                weather.setHumidity((String) firstLive.get("humidity"));
                weather.setReporttime((String) firstLive.get("reporttime"));
                weather.setCreateTime((String) firstLive.get("reporttime"));
                weather.setInfocode((String) ret.get("infocode"));

                System.out.println("天气信息：" + weather);
                int i = weatherService.insertWeather(weather);
                System.out.println("插入结果：" + i);
            }
        }

        return ret;
    }

    // 获取所有天气记录
    @GetMapping("/")
    public List<Weather> getAllWeather() {
        return weatherService.list();  // 使用 MyBatis-Plus 提供的 list() 方法获取所有记录
    }

    // 添加新的天气记录
    @PostMapping("/")
    public Weather addWeather(@RequestBody Weather weather) {
        weatherService.save(weather);  // 使用 MyBatis-Plus 提供的 save() 方法插入数据
        return weather;  // 返回插入的数据
    }

    // 更新天气记录
    @PutMapping("/{id}")
    public Weather updateWeather(@PathVariable BigInteger id, @RequestBody Weather weather) {
        weather.setId(id);  // 设置更新的天气记录的 ID
        weatherService.updateById(weather);  // 使用 MyBatis-Plus 提供的 updateById() 方法更新数据
        return weather;  // 返回更新的数据
    }

    // 删除天气记录
    @DeleteMapping("/{id}")
    public boolean deleteWeather(@PathVariable BigInteger id) {
        return weatherService.removeById(id);  // 使用 MyBatis-Plus 提供的 removeById() 方法删除数据
    }

    @PostMapping("/update")
    public String updateWeather(@RequestBody Map<String, Object> data) {
        System.out.println("温度: " + data.get("temperature"));
        System.out.println("湿度: " + data.get("humidity"));
        return "success";
    }

}
